﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using NS.Utilities;
using NS.BLL.Systems;
using BLL;
using Model;
using WechatUtility;

public partial class Platform_CarTypeManage : System.Web.UI.Page
{
    private string ParentID = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ParentID"] != null)
        {
            ParentID = Request.QueryString["ParentID"];
        }
        if (!Page.IsPostBack)
        {
            GridViewDataBind();
            LabPath.Text = CreatePath(ParentID);
        }
    }

    private string CreatePath(string ParentID)
    {
        StringBuilder MenuPath = new StringBuilder("<a href=CarTypeManage.aspx?mid=" + Request.QueryString["mid"] + "&ParentID=0>所有车型</a>>>");
        CarType Model = CarTypeService.GetCarTypeByTypeID(Convert.ToInt64(ParentID));
        if (Model != null)
        {
            string[] MyArray = (Model.ParentPath + "," + ParentID).Split(',');
            foreach (string TypeID in MyArray)
            {
                if (TypeID != "0")
                {
                    string strTypeName = UtilityService.GetFieldValueByPK("CarType", "TypeName", "TypeID", TypeID)[0].ToString();
                    MenuPath.Append("<a href=CarTypeManage.aspx?mid=" + Request.QueryString["mid"] + "&ParentID=" + TypeID + ">" + strTypeName + "</a>>>");
                }
            }
        }
        return MenuPath.ToString();
    }

    private void GridViewDataBind()
    {
        FGridView1.SQLString = CarTypeService.GetAllCarType_SQL(ParentID);
        FGridView1.PKey = "TypeID";
        FGridView1.OrderName = "OrderID";
        FGridView1.BindData();
        FGridView1.AddControl();
    }
    protected void btnBindData_Click(object sender, EventArgs e)
    {
        GridViewDataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        CarType Model = new CarType();
        Model.TypeName = txtTypeName.Text.Trim();
        Model.Letter = StringHelper.ChineseCaps(Model.TypeName.Substring(0, 1));
        string logo = FileHelper.SaveFile(ImageUpload, "../wechat/carlogo/");
        Model.Logo = logo.Substring(10);
        Model.OrderID = Convert.ToInt32(txtOrderID.Text.Trim());
        Model.ParentID = Convert.ToInt64(ParentID);
        if (ParentID != "0")
        {
            object[] ParentInfo = UtilityService.GetFieldValueByPK("CarType", "ParentPath,Depth", "TypeID", ParentID.ToString());
            Model.ParentPath = ParentInfo[0].ToString() + "," + ParentID;
            Model.Depth = Convert.ToInt32(ParentInfo[1]) + 1;
        }
        else
        {
            Model.ParentPath = "0";
            Model.Depth = 0;
        }
        if (CarTypeService.AddCarType(Model) > 0)
        {
            GridViewDataBind();
            txtTypeName.Text = string.Empty;
            txtOrderID.Text = (Model.OrderID + 1).ToString();
        }
        else
        {
            MessageBox.ShowSaveError(this);
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string TypeID = ((Button)sender).CommandArgument.ToString();
        CarTypeService.DeleteCarTypeByTypeID(Convert.ToInt64(TypeID));
        GridViewDataBind();
    }
}